package com.zdp.dubbo.provider.service;

import com.zdp.dubbo.service.UserService;
import domain.User;
import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

/**
 * @author ZhengDp
 * @Date 2023/6/3 17:01
 */
@DubboService
public class UserServiceImpl implements UserService {

    @Override
    public User getUser(String uid) {
        User ZhengDP = new User(uid, "ZhengDP");
        return ZhengDP;
    }

    @Override
    public void sayHelloServerStream(String uid, StreamObserver<String> response) throws InterruptedException {
        System.out.println("服务端 接收到 客户端请求，Uid : " + uid);
        // 响应数据
        response.onNext("data1");
        System.out.println("进行其他处理 ....");
        response.onNext("data2");
        Thread.sleep(2000L);
        // 关闭响应流
        response.onCompleted();
    }

    @Override
    public StreamObserver<String> sayHelloClientStream(StreamObserver<String> request) {
        StreamObserver<String> res = new StreamObserver<String>() {
            @Override
            public void onNext(String data) {
                System.out.println("接收到客户端传递数据 : " + data);
                // 响应数据给 客户端
                request.onNext("response : " + data);
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {

            }
        };

        return res;
    }
}
